<?php

namespace longhu\command;

use longhu\exceptions\FiledNotFoundException;
use longhu\generate\factory\Dao;
use longhu\MyAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\helper\Str;

class CreateDaoCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('create:dao')
            ->addArgument('module', Argument::REQUIRED, 'module name')
            ->addArgument('dao', Argument::REQUIRED, 'dao name')
            ->setDescription('create dao');
    }

    protected function execute(Input $input, Output $output)
    {
        $dao = ucfirst($input->getArgument('dao'));
        $module = ucfirst($input->getArgument('module'));
        $daoName = $dao.'Dao';
        $params = [
            'dao' => MyAdmin::$root.'\\'  . 'common\\dao\\' . $daoName,
            'model' => MyAdmin::$root.'\\' . $module . '\\model\\' . $dao,
        ];
        if (!file_exists(MyAdmin::getModuleModelDirectory($module) . $dao . '.php')) {
            throw new FiledNotFoundException($params['model'].'is not find,ps create model');
        }
        $modelFile= MyAdmin::getDaoDirectory() . $daoName . '.php';
        $asn = 'Y';
        if (file_exists($modelFile)) {
            $asn = $this->output->ask($this->input, "Model File {$daoName} already exists.Are you sure to overwrite, the content will be lost(Y/N)");
        }
        if (strtolower($asn) == 'n') {
            exit(0);
        }
        (new Dao())->done($params);
    }
}